package com.msb.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.msb.mall.product.entity.BrandEntity;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.BrandService;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.product.dao.CategoryBrandRelationDao;
import com.msb.mall.product.entity.CategoryBrandRelationEntity;
import com.msb.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandService brandService;
    @Autowired
    CategoryService categoryService;

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据品牌id和三级分类的id，查询出对应的name
     *
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //获取品牌id和三级分类的id
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        //根据id查询出对应的实体类
        CategoryEntity categoryentity = categoryService.getById(catelogId);
        BrandEntity beandentity = brandService.getById(brandId);

        //把查询出来的name封装到，三级分类品牌关联表的实体类中
        categoryBrandRelation.setCatelogName(categoryentity.getName());
        categoryBrandRelation.setBrandName(beandentity.getName());

        //增加到三级分类品牌关联表中
        this.save(categoryBrandRelation);


    }

    /**
     * 品牌表更新了，对应的品牌三级分类关联表也要相应的更新
     *
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrandName(Long brandId, String name) {

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity
                , new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

    }


    /**
     * 三级分类表更新了，对应的品牌三级分类关联表也要相应的更新
     *
     * @param catId
     * @param name
     */
    @Override
    public void updateCategoryName(Long catId, String name) {

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();

        categoryBrandRelationEntity.setCatelogId(catId);
        categoryBrandRelationEntity.setCatelogName(name);


        this.update(categoryBrandRelationEntity
                , new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    }


    @Override
    public List<CategoryBrandRelationEntity> categoryBrandRelation(Long catId) {


         List<CategoryBrandRelationEntity> list
                = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return list;
    }
}


